# coding: UTF-8
# Filename : member_action.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl
from django.shortcuts import render_to_response
import member_manage

#获取申请会员所需表单项
def get_apply_columns(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    user_id = httputils.post(request, 'user_id')
    cus_id = httputils.post(request, 'cus_id')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    columns = member_manage.get_apply_columns(user_id, cus_id)
    return render_to_response('member/columns.xml',{'columns':columns}, mimetype="application/xhtml+xml")
    

#申请会员
def apply_member(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    name = httputils.post(request, 'name')
    gender = httputils.post(request, 'gender')
    email = httputils.post(request, 'email')
    birthday = httputils.post(request, 'birthday')
    idcard = httputils.post(request, 'idcard')
    user_id = httputils.post(request, 'user_id')
    cus_id = httputils.post(request, 'cus_id')
    
    is_exist = member_manage.check_is_exist(user_id, cus_id)
    if not is_exist:
        msg = {'msg':'抱歉没有此用户或商家相关信息!'}
        return render_to_response('member/apply_error.xml', msg, mimetype="application/xhtml+xml")
    
    is_ok = member_manage.insert_member(name, gender, email, birthday, idcard, user_id, cus_id)
    if is_ok:
        join_way = member_manage.get_join_way(cus_id)
        if join_way in (0,1):
            success_info = '1'
            msg = '申请成功,恭喜您成为会员!'
        else:
            success_info = '2'
            msg = '申请成功,我们会尽快审核,感谢您的支持!'
        return render_to_response('member/apply_success.xml',{'success':success_info, 'msg':msg}, mimetype="application/xhtml+xml")
    else:
        msg = {'msg':'申请会员失败'}
        return render_to_response('member/apply_error.xml', msg, mimetype="application/xhtml+xml")

#我的会员卡
def member_info(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    cus_id = httputils.post(request, 'cus_id')
    user_id = httputils.post(request, 'user_id')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    mc = member_manage.get_member_info(user_id, cus_id)
    return render_to_response('member/success.xml', {'mc':mc}, mimetype="application/xhtml+xml")
    
#会员体系声明
def member_statement(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    cus_id = httputils.post(request, 'cus_id')
    user_id = httputils.post(request, 'user_id')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    statement = member_manage.get_member_statement(user_id, cus_id)
    return render_to_response('member/member_statement.xml', {'statement':statement}, mimetype="application/xhtml+xml")
    
#检测是否已经审核
def get_state(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    cus_id = httputils.post(request, 'cus_id')
    user_id = httputils.post(request, 'user_id')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    ms = member_manage.get_state(user_id, cus_id)
    return render_to_response('member/member_state.xml', {'ms':ms}, mimetype="application/xhtml+xml")

#意见反馈
def feedback(request):
    cus_id = httputils.post(request, 'cus_id')
    user_id = httputils.post(request, 'user_id')
    phone_num = httputils.post(request, 'phone_num')
    content = httputils.post(request, 'content')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    rst = member_manage.feedback(user_id, cus_id, phone_num, content)
    if rst: msg = 1
    else: msg = 0
    
    return render_to_response('member/feedback.xml', {'msg':msg}, mimetype="application/xhtml+xml")
    
    